package com.workflow.action;

import java.util.List;

import com.base.bean.Cardinfo;
import com.base.service.CardinfoService;
import com.base.service.DictService;
import com.common.bean.Datadict;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Exitdeptdetail;
import com.workflow.service.ExitcardService;
import com.workflow.service.ExitdeptdetailService;

public class ExitcardManagerAction extends AbstractActionSupport implements
		Preparable {
	private static final long serialVersionUID = -1106841141177775867L;

	private ExitcardService exitcardService;
	
	private CardinfoService cardinfoService;
	
	private ExitdeptdetailService exitdeptdetailService;
	
	private DictService dictService;
	
	private Exitdeptdetail exitdeptdetail;
	
	private Cardinfo cardinfo;
	
	private String exitdetailno;
	
	private String cardno;
	
	private PagerModel pm;
	
	private long money;//面额
	
	private boolean selected;//是否是查看已选卡标识
	
	private List<Datadict> datadictByBasetypelist;
	
	private List<Datadict> datadictByAreatypelist;
	
	private List<Datadict> datadictByFuntypelist;
	
	private Datadict datadictByBasetype;

	private Datadict datadictByAreatype;

	private Datadict datadictByFuntype;
	
	@SuppressWarnings("unchecked")
	public void prepare() throws Exception {
		datadictByBasetypelist = dictService.findDictDetail(StringToken.DICT_CARD_BASETYPE);
		datadictByAreatypelist = dictService.findDictDetail(StringToken.DICT_CARD_AREATYPE);
		datadictByFuntypelist = dictService.findDictDetail(StringToken.DICT_CARD_FUNTYPE);
	}
	
	@Override
	public String execute() throws Exception {
		exitdeptdetail = exitdeptdetailService.findExitdetailno(exitdetailno);
		return SUCCESS;
	}
	
	public String addExitcard() throws Exception {
		exitcardService.addExitcard(cardno, exitdetailno);
		return SUCCESS;
	}
	
	public String findCardinfoForExitdetailno() throws Exception {
		Personnel personnel = (Personnel) this.getSession().get(StringToken.CURRENT_LOGIN);
		selected = false;
		exitdeptdetail = exitdeptdetailService.findExitdetailno(exitdetailno);
		//根据三种类型、面额,子卡类型（志号）选卡（状态为已入库，切所属部门为当前登录用户所属部门） 
		pm = cardinfoService.findCardinfoForPagerWith(datadictByBasetype.getTypeid(), 
				datadictByAreatype.getTypeid(), datadictByFuntype.getTypeid(),
				StringToken.DICT_CARDSTATUS_STORE, money,
				exitdeptdetail.getCardsubtype().getCardsubtypeno(),personnel.getDepartment().getDeptcode());
		return SUCCESS;
	}
	
	public String findCardinfoExitSelected() throws Exception {
		Personnel personnel = (Personnel) this.getSession().get(StringToken.CURRENT_LOGIN);
		selected = true;
		exitdeptdetail = exitdeptdetailService.findExitdetailno(exitdetailno);
//		根据出库明细号（状态为流转）当前部门(出库单所属部门) 子卡类型 
		pm = cardinfoService.findCardinfoForPagerWithBelongto(exitdetailno, StringToken.DICT_CARDSTATUS_LOCK,
				exitdeptdetail.getCardsubtype().getCardsubtypeno(),personnel.getDepartment().getDeptcode());
		return SUCCESS;
	}
	
	public String delExitcard() throws Exception {
		exitcardService.removeExitcard(cardno);
		cardinfo = cardinfoService.findCardinfo(cardno);
		if(!cardinfo.getBelongto().equals(exitdetailno)) {
			throw new RuntimeException("非法操作！");
		}
		Datadict datadictByStatus = new Datadict();
		//置卡状态为库存 状态编号 76l
		datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_STORE);
		cardinfo.setDatadictByStatus(datadictByStatus);
		cardinfo.setBelongto("");//取消所属状态，临时
		cardinfoService.updateCardinfo(cardinfo);
		return SUCCESS;
	}

	/**
	 * @return the cardinfo
	 */
	public Cardinfo getCardinfo() {
		return cardinfo;
	}

	/**
	 * @param cardinfo the cardinfo to set
	 */
	public void setCardinfo(Cardinfo cardinfo) {
		this.cardinfo = cardinfo;
	}

	/**
	 * @return the cardinfoService
	 */
	public CardinfoService getCardinfoService() {
		return cardinfoService;
	}

	/**
	 * @param cardinfoService the cardinfoService to set
	 */
	public void setCardinfoService(CardinfoService cardinfoService) {
		this.cardinfoService = cardinfoService;
	}

	/**
	 * @return the cardno
	 */
	public String getCardno() {
		return cardno;
	}

	/**
	 * @param cardno the cardno to set
	 */
	public void setCardno(String cardno) {
		this.cardno = cardno;
	}

	/**
	 * @return the datadictByAreatype
	 */
	public Datadict getDatadictByAreatype() {
		return datadictByAreatype;
	}

	/**
	 * @param datadictByAreatype the datadictByAreatype to set
	 */
	public void setDatadictByAreatype(Datadict datadictByAreatype) {
		this.datadictByAreatype = datadictByAreatype;
	}

	/**
	 * @return the datadictByAreatypelist
	 */
	public List<Datadict> getDatadictByAreatypelist() {
		return datadictByAreatypelist;
	}

	/**
	 * @param datadictByAreatypelist the datadictByAreatypelist to set
	 */
	public void setDatadictByAreatypelist(List<Datadict> datadictByAreatypelist) {
		this.datadictByAreatypelist = datadictByAreatypelist;
	}

	/**
	 * @return the datadictByBasetype
	 */
	public Datadict getDatadictByBasetype() {
		return datadictByBasetype;
	}

	/**
	 * @param datadictByBasetype the datadictByBasetype to set
	 */
	public void setDatadictByBasetype(Datadict datadictByBasetype) {
		this.datadictByBasetype = datadictByBasetype;
	}

	/**
	 * @return the datadictByBasetypelist
	 */
	public List<Datadict> getDatadictByBasetypelist() {
		return datadictByBasetypelist;
	}

	/**
	 * @param datadictByBasetypelist the datadictByBasetypelist to set
	 */
	public void setDatadictByBasetypelist(List<Datadict> datadictByBasetypelist) {
		this.datadictByBasetypelist = datadictByBasetypelist;
	}

	/**
	 * @return the datadictByFuntype
	 */
	public Datadict getDatadictByFuntype() {
		return datadictByFuntype;
	}

	/**
	 * @param datadictByFuntype the datadictByFuntype to set
	 */
	public void setDatadictByFuntype(Datadict datadictByFuntype) {
		this.datadictByFuntype = datadictByFuntype;
	}

	/**
	 * @return the datadictByFuntypelist
	 */
	public List<Datadict> getDatadictByFuntypelist() {
		return datadictByFuntypelist;
	}

	/**
	 * @param datadictByFuntypelist the datadictByFuntypelist to set
	 */
	public void setDatadictByFuntypelist(List<Datadict> datadictByFuntypelist) {
		this.datadictByFuntypelist = datadictByFuntypelist;
	}

	/**
	 * @return the dictService
	 */
	public DictService getDictService() {
		return dictService;
	}

	/**
	 * @param dictService the dictService to set
	 */
	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}

	/**
	 * @return the exitcardService
	 */
	public ExitcardService getExitcardService() {
		return exitcardService;
	}

	/**
	 * @param exitcardService the exitcardService to set
	 */
	public void setExitcardService(ExitcardService exitcardService) {
		this.exitcardService = exitcardService;
	}

	/**
	 * @return the exitdeptdetail
	 */
	public Exitdeptdetail getExitdeptdetail() {
		return exitdeptdetail;
	}

	/**
	 * @param exitdeptdetail the exitdeptdetail to set
	 */
	public void setExitdeptdetail(Exitdeptdetail exitdeptdetail) {
		this.exitdeptdetail = exitdeptdetail;
	}

	/**
	 * @return the exitdeptdetailService
	 */
	public ExitdeptdetailService getExitdeptdetailService() {
		return exitdeptdetailService;
	}

	/**
	 * @param exitdeptdetailService the exitdeptdetailService to set
	 */
	public void setExitdeptdetailService(ExitdeptdetailService exitdeptdetailService) {
		this.exitdeptdetailService = exitdeptdetailService;
	}

	/**
	 * @return the exitdetailno
	 */
	public String getExitdetailno() {
		return exitdetailno;
	}

	/**
	 * @param exitdetailno the exitdetailno to set
	 */
	public void setExitdetailno(String exitdetailno) {
		this.exitdetailno = exitdetailno;
	}

	/**
	 * @return the pm
	 */
	public PagerModel getPm() {
		return pm;
	}

	/**
	 * @param pm the pm to set
	 */
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the money
	 */
	public long getMoney() {
		return money;
	}

	/**
	 * @param money the money to set
	 */
	public void setMoney(long money) {
		this.money = money;
	}

	/**
	 * @return the selected
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * @param selected the selected to set
	 */
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	
}
